﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using CloudSoft.Extensions;

namespace ERPStore.Exporter.Models.LeadSources
{
	public class TwengaSource : ERPStore.Models.LeadSourceBase
	{
		public override string Id
		{
			get { return "399F815C-E82F-4E84-9291-1BAB90333812"; }
		}

		public override string Name
		{
			get { return "Twenga"; }
		}

		public override string Description
		{
			get { return "Comparateur de prix Twenga"; }
		}

		public override string TrackSourceId
		{
			get { return "twenga"; }
		}

		public override string Medium
		{
			get { return "cpc"; }
		}

		public override ERPStore.Models.LeadSourceType LeadSourceType
		{
			get { return ERPStore.Models.LeadSourceType.PriceComparator; }
		}

		public override bool Match(Uri sourceUri, Uri landingUri, out string keywords, out string subHost, out decimal amount)
		{
			keywords = null;
			subHost = null;
			amount = 0.19m;
			var landingPrms = landingUri.GetParameters();

			if (landingPrms.AllKeys.Contains("gclid")) // Lien sponso 
			{
				return false;
			}
			else if (landingPrms.AllKeys.Contains("estid"))
			{
				var estid = landingPrms["estid"];
				if (estid.Equals("twenga", StringComparison.InvariantCultureIgnoreCase))
				{
					subHost = "No referer";
					return true;
				}
			}
			else if (sourceUri.Host.ToLower().IndexOf("twenga") != -1)
			{
				return true;
			}

			return false;
		}

		protected override string GetKeywords(Uri uri)
		{
			if (uri == null)
			{
				return null;
			}

			var prms = uri.GetParameters();

			var k = prms["ms"];
			if (k == null)
			{
				if (uri.Host.IndexOf("webmarchand.com") > -1)
				{
					if (uri.PathAndQuery.IndexOf("/") != -1)
					{
						var parts = uri.PathAndQuery.Split('/').ToList();
						foreach (var part in parts)
						{
							if (part.Equals("mot"))
							{
								k = parts[parts.IndexOf(part) + 1];
								k = k.Replace("_", " ");
								break;
							}
						}
					}
				}
			}

			return k.Trim();
		}

	}
}
